import store from './store'
import Rwebsocket from 'reconnecting-websocket'
import Vue from 'vue'
// reconnecting websocket是一个javascript封闭的websocket库，非常小，压缩后仅有3KB。它的功能是当websocket断线后自动帮你重连
// 引入BASE64转码编码函数/
const Base64 = require('js-base64').Base64
let Socket = ''
/**
 * 建立websocket连接
 */
var lockReconnect = false
// var wsUrl = 'ws://175.27.243.236:6060/test'
// 备用ip
// var wsUrl = 'ws://112.124.67.119:6060/test'
// var wsUrl = 'ws://192.168.199.205:6060/test'
var wsUrl = 'ws://47.99.175.180:6060/test'
// var wsUrl = 'ws://192.168.199.128:6060/test'
// 获取本机的ip和端口号   13732473813
var IP = window.location.hostname
// var port = window.location.port
var port = '22'
export const createSocket = url => {
  try {
    if ('WebSocket' in window) {
      Socket && Socket.close()
      console.log('建立websocket连接')
      Socket = new Rwebsocket(url)
    }
    Socket.onopen = onopenWS
    Socket.onmessage = onmessageWS
    Socket.onerror = onerrorWS
    Socket.onclose = oncloseWS
  } catch (e) {
    reconnect(url)
  }
}
export const onopenWS = a => {
  console.log('链接打开')
  heartCheck.reset().start() // 心跳检测重置
  // 使用token登录
  // var messageLOGIN = {
  //   LOGIN: {
  //     TOKEN: 'FEWHIFHWEIFHOQY9RR31R21FF9ML9JZH9HPH6B02',
  //     PRIVATE_IP: '192.168.0.205',
  //     PRIVATE_PORT: '62809',
  //     ENCODING: 'UTF-8'
  //   }
  // }
  // var messageJsonLOGIN = JSON.stringify(messageLOGIN)
  // Socket.send(messageJsonLOGIN)
  // 使用手机号登录
  var messageAUTHREQ = {
    AUTH_REQ: {
      ACCOUNT: store.state.Num,
      PASSWORD: store.state.Password,
      'TOKEN TYPE': store.state.type,
      PRIVATE_IP: IP,
      PRIVATE_PORT: port,
      ENCODING: 'UTF-8'
    }
  }
  var messageJsonAUTHREQ = JSON.stringify(messageAUTHREQ)
  console.log(123333333333)
  if (store.state.Num === '') {
    console.log('token为空')
  } else {
    Socket.send(messageJsonAUTHREQ)
  }
}
export const socketsend = msg => {
  var messageROUTER = {
    ROUTER: {
      TOKEN: store.state.token,
      'PEER TOKEN': store.state.peerToken,
      CONTENT: Base64.encode('req_all_trans_data'),
      TYPE: 'TXT',
      ENCODING: 'BASE64'
    }
  }
  var messageJsonROUTER = JSON.stringify(messageROUTER)
  Socket.send(messageJsonROUTER)
  console.log('刷新刷新')
}

export const socketsendshare1 = msg => {
  var messageSHARE1 = {
    ROUTER: {
      TOKEN: store.state.token,
      'PEER TOKEN': msg,
      CONTENT: Base64.encode('req_all_trans_data'),
      TYPE: 'TXT',
      ENCODING: 'BASE64'
    }
  }
  var messageJsonSHARE1 = JSON.stringify(messageSHARE1)
  Socket.send(messageJsonSHARE1)
  console.log('刷新刷新')
}
export const socketsendaccount = msg => {
  var messageSHARE1 = {
    ROUTER: {
      TOKEN: store.state.token,
      'PEER TOKEN': msg,
      CONTENT: Base64.encode('req_account_data'),
      TYPE: 'TXT',
      ENCODING: 'BASE64'
    }
  }
  var messageJsonSHARE1 = JSON.stringify(messageSHARE1)
  Socket.send(messageJsonSHARE1)
}
export const socketsend1 = msg => {
  var messageLOGIN1 = {
    LOGIN: {
      PRIVATE_IP: IP,
      PRIVATE_PORT: port,
      ENCODING: 'UTF-8'
    }
  }
  var messageJsonLOGIN1 = JSON.stringify(messageLOGIN1)
  Socket.send(messageJsonLOGIN1)
}
// 新增发送关注订阅接口
export const socketsendpromote = msg => {
  var messagePromote = {
    PROMOTE: {
      TOKEN: store.state.token,
      'TOKEN TYPE': store.state.type,
      PRIVATE_IP: IP,
      PRIVATE_PORT: port,
      ENCODING: 'UTF-8',
      ACCOUNT: '13357146917'
    }
  }
  var messageJsonPromote = JSON.stringify(messagePromote)
  Socket.send(messageJsonPromote)
}
export const socketsendpromotionlist = msg => {
  var messagePromotionList = {
    'PROMOTION List': {
      TOKEN: store.state.token,
      PRIVATE_IP: IP,
      PRIVATE_PORT: port,
      ENCODING: 'UTF-8'
    }
  }
  var messageJsonPromotionList = JSON.stringify(messagePromotionList)
  Socket.send(messageJsonPromotionList)
}
export const socketsendaddcontact = msg => {
  var messageAddContact = {
    'ADD CONTACT': {
      TOKEN: store.state.token,
      'TOKEN TYPE': store.state.type,
      PRIVATE_IP: IP,
      PRIVATE_PORT: port,
      CONTACT: store.state.addcontact // 对方的手机号
    }
  }
  var messageJsonAddContact = JSON.stringify(messageAddContact)
  Socket.send(messageJsonAddContact)
}
export const socketsendremovecontact = msg => {
  var messageRemoveContact = {
    'REMOVE CONTACT': {
      TOKEN: store.state.token,
      'TOKEN TYPE': store.state.type,
      PRIVATE_IP: IP,
      PRIVATE_PORT: port,
      CONTACT: store.state.addcontact // 对方的手机号
    }
  }
  var messageJsonRemoveContact = JSON.stringify(messageRemoveContact)
  Socket.send(messageJsonRemoveContact)
}
export const socketsendgetcontact = msg => {
  var messageGetContact = {
    'GET CONTACTS': {
      TOKEN: store.state.token,
      'TOKEN TYPE': store.state.type,
      PRIVATE_IP: IP,
      PRIVATE_PORT: port
    }
  }
  var messageJsonGetContact = JSON.stringify(messageGetContact)
  Socket.send(messageJsonGetContact)
}
export const socketsendfollow = msg => {
  var messageFollow = {
    FOLLOW: {
      TOKEN: store.state.token,
      'TOKEN TYPE': store.state.type,
      PRIVATE_IP: IP,
      PRIVATE_PORT: port,
      FOLLOWING: msg // 对方手机号
    }
  }
  var messageJsonFollow = JSON.stringify(messageFollow)
  Socket.send(messageJsonFollow)
}
export const socketsendunfollow = msg => {
  var messageUnFollow = {
    UNFOLLOW: {
      TOKEN: store.state.token,
      'TOKEN TYPE': store.state.type,
      PRIVATE_IP: IP,
      PRIVATE_PORT: port,
      FOLLOWING: msg // 对方手机号
    }
  }
  var messageJsonUnFollow = JSON.stringify(messageUnFollow)
  Socket.send(messageJsonUnFollow)
}
export const socketsendgetfollowings = msg => {
  var messageGetFollowings = {
    'GET FOLLOWINGS': {
      TOKEN: store.state.token,
      'TOKEN TYPE': store.state.type,
      PRIVATE_IP: IP,
      PRIVATE_PORT: port
    }
  }
  var messageJsonGetFollowings = JSON.stringify(messageGetFollowings)
  Socket.send(messageJsonGetFollowings)
}
export const socketsendshare = msg => {
  var messageShare = {
    SHARE: {
      TOKEN: store.state.token,
      'TOKEN TYPE': store.state.type,
      'GROUP NAME': 'CONTACTS',
      CONTENT: Base64.encode('req_all_trans_data'),
      PRIVATE_IP: IP,
      PRIVATE_PORT: port
    }
  }
  var messageJsonShare = JSON.stringify(messageShare)
  Socket.send(messageJsonShare)
}
// -------------------------------
export const onmessageWS = e => {
  heartCheck.reset().start()
  console.log(e.data)
  // store.state.token = e.data.LOGIN.TOKEN
  var obj = JSON.parse(e.data)
  // console.log(obj)
  // var sharemess = Base64.decode(obj.SHARE.CONTENT)
  // console.log(sharemess)
  if ('AUTH_ANS' in obj) {
    if (obj.AUTH_ANS.TOKEN === '') {
      store.state.token = 0
      console.log(2222222)
    } else {
      if (obj.AUTH_ANS.STATUS === '1') {
        store.state.token = obj.AUTH_ANS.TOKEN
        console.log('AUTH_ANSAUTH_ANSAUTH_ANSAUTH_ANSAUTH_ANS')
        var messageLOGINMB = {
          LOGIN: {
            TOKEN: store.state.token,
            'TOKEN TYPE': store.state.type,
            PRIVATE_IP: IP,
            PRIVATE_PORT: port,
            ENCODING: 'UTF-8'
          }
        }
        var messageJsonLOGINMB = JSON.stringify(messageLOGINMB)
        Socket.send(messageJsonLOGINMB)
      } else if (obj.AUTH_ANS.STATUS === '-1') {
        console.log(1111111111111)
        store.state.token = '-1'
        console.log(store.state.token)
      } else if (obj.AUTH_ANS.STATUS === '-2') {
        store.state.token = '-2'
      } else if (obj.AUTH_ANS.STATUS === '-3') {
        store.state.token = '-3'
      } else {
        store.state.token = '-4'
      }

      // var messageTOKENLIST = {
      //   'TOKEN LIST': {
      //     TOKEN: store.state.token,
      //     ENCODING: 'UTF-8'
      //   }
      // }
      // var messageJsonTOKENLIST = JSON.stringify(messageTOKENLIST)
      // Socket.send(messageJsonTOKENLIST)
    }
  } else if ('LOGIN' in obj) {
    console.log('握手')
    store.state.loginBtnstatus = '1'
    store.state.publicIP = obj.LOGIN.PUBLIC_IP
    store.state.publicPort = obj.LOGIN.PUBLIC_PORT
    store.state.token = obj.LOGIN.TOKEN
    var messageSHAKEHANDS = {
      'SHAKE HANDS': {
        TOKEN: store.state.token,
        PUBLIC_IP: store.state.publicIP,
        PUBLIC_PORT: store.state.publicPort,
        ENCODING: 'UTF-8'
      }
    }
    var messageJsonSHAKEHANDS = JSON.stringify(messageSHAKEHANDS)
    Socket.send(messageJsonSHAKEHANDS)
    var messageTOKENLIST = {
      'TOKEN LIST': {
        TOKEN: store.state.token,
        ENCODING: 'UTF-8'
      }
    }
    var messageJsonTOKENLIST = JSON.stringify(messageTOKENLIST)
    Socket.send(messageJsonTOKENLIST)
  } else if (['TOKEN LIST'] in obj) {
    for (let i = 0; i < 3; i++) {
      if (obj['TOKEN LIST']['TOKEN' + i]['TOKEN TYPE'] === '0') {
        store.state.peerToken = obj['TOKEN LIST']['TOKEN' + i]['TOKEN STRING']
        if (obj['TOKEN LIST']['TOKEN' + i].PRIVATE_IP === '') {
          store.state.judgeLoginStatus = 'lose'
        } else {
          store.state.judgeLoginStatus = 'success'
        }
      }
    }
    // if (obj['TOKEN LIST'].TOKEN0['TOKEN TYPE'] === '0') {
    //   store.state.peerToken = obj['TOKEN LIST'].TOKEN0['TOKEN STRING']
    //   if (obj['TOKEN LIST'].TOKEN0.PRIVATE_IP === '') {
    //     store.state.judgeLoginStatus = 'lose'
    //   }
    // } else if (obj['TOKEN LIST'].TOKEN1['TOKEN TYPE'] === '0') {
    //   store.state.peerToken = obj['TOKEN LIST'].TOKEN1['TOKEN STRING']
    //   if (obj['TOKEN LIST'].TOKEN1.PRIVATE_IP === '') {
    //     store.state.judgeLoginStatus = 'lose'
    //   }
    // } else if (obj['TOKEN LIST'].TOKEN2['TOKEN TYPE'] === '0') {
    //   store.state.peerToken = obj['TOKEN LIST'].TOKEN2['TOKEN STRING']
    //   if (obj['TOKEN LIST'].TOKEN2.PRIVATE_IP === '') {
    //     store.state.judgeLoginStatus = 'lose'
    //   }
    // } else {
    //   store.state.judgeLoginStatus = 'success'
    // }
  } else if (['PROMOTION LIST'] in obj) {
    store.state.promotiontoken = []
    store.state.promotionaccount = []
    console.log(obj['PROMOTION LIST'])
    for (let i = 0; i < Object.keys(obj['PROMOTION LIST']).length; i++) {
      if (obj['PROMOTION LIST']['ACCOUNT' + i].TOKEN === '') {
        console.log(
          '账户' + obj['PROMOTION LIST']['ACCOUNT' + i].ACCOUNT + '未登录'
        )
        store.state.accountStatus = '未登录'
      } else {
        console.log(obj['PROMOTION LIST']['ACCOUNT' + i].TOKEN)
        store.state.promotiontoken.push(
          obj['PROMOTION LIST']['ACCOUNT' + i].TOKEN
        )
        store.state.promotionaccount.push(
          obj['PROMOTION LIST']['ACCOUNT' + i].ACCOUNT
        )
        console.log(store.state.promotionaccount)
      }
    }
  } else if ('SHARE' in obj) {
    // 关注者分享的信息
    var sharemess = Base64.decode(obj.SHARE.CONTENT)
    if (obj.SHARE['GROUP NAME'] === 'FOLLOWERS') {
      store.state.groupname = 'FOLLOWERS'
    } else if (obj.SHARE['GROUP NAME'] === 'PROMOTE') {
      store.state.groupname = 'PROMOTE'
    }
    console.log('这个是收到的分享的数据')
    console.log(store.state.groupname)
    console.log(sharemess)
    console.log(obj.SHARE.TOKEN)
    // if (store.state.groupname === 'PROMOTE') {
    //   if (obj.SHARE.TOKEN === '227094f673d74e6f08a69a425832ac17') {
    //     store.state.x0++
    //   } else if (obj.SHARE.TOKEN === 'a619ba8295ab02cfcd07b52b0d6f0f45') {
    //     store.state.x1++
    //   } else if (obj.SHARE.TOKEN === 'ead0c0f0dfe9655057d97039225bde65') {
    //     store.state.x2++
    //   } else if (obj.SHARE.TOKEN === 'bac2eb0822e2cd22f10338da52558ceb') {
    //     store.state.x3++
    //   } else if (obj.SHARE.TOKEN === '440561b4af12c6ef33b8bc2d69afd68f') {
    //     store.state.x4++
    //   } else if (obj.SHARE.TOKEN === '99a65309d59f2a4e6bb8da3592600e08') {
    //     store.state.x5++
    //   } else if (obj.SHARE.TOKEN === 'af6d628ec6acb8968b8f2e6a80b40789') {
    //     store.state.x6++
    //   } else if (obj.SHARE.TOKEN === '282e0995c43f3040023a89a44418af2c') {
    //     store.state.x7++
    //   } else if (obj.SHARE.TOKEN === '277edfb74bc841e3b10431aff84a4113') {
    //     store.state.x8++
    //   }
    // }
    if (sharemess === 'account records begin') {
      // if (Base64.decode(obj.SHARE.CONTENT) === '123') {
      //   alert('123')
      // }
      // store.state.accountdatas = []
      store.state.mess1 = 'data_account records begin'
    } else if (sharemess === 'account records change begin') {
      store.state.mess1 = 'data_account records change begin'
    } else if (sharemess === 'transaction records begin') {
      if (obj.SHARE.TOKEN === store.state.promotiontoken[store.state.zheI]) {
        store.state.transactiondatas = []
      }
      store.state.mess1 = 'data_transaction records begin'
      // } else if (sharemess === 'transjob records begin') {
      //   store.state.transjobdatas = []
      //   store.state.mess1 = 'data_transjob records begin'
    } else if (sharemess === 'transaction records profit begin') {
      store.state.mess1 = 'data_transaction records profit begin'
    } else if (sharemess === 'his_asset records begin') {
      store.state.sharenetWorthCurve = []
      store.state.shareNET = []
      store.state.shareTIME = []
      store.state.mess1 = 'data_his_asset records begin'
    } else if (sharemess === 'push message begin') {
      store.state.mess1 = 'data_push message begin'
    } else {
      if (
        store.state.mess1 === 'data_account records begin' &&
        store.state.groupname === 'FOLLOWERS'
      ) {
        var accs = []
        var accs00 = []
        accs.push(...JSON.parse(sharemess))
        for (let m = 0; m < store.state.accountdatas.length; m++) {
          accs00.push(store.state.accountdatas[m].account)
        }
        console.log(accs[0].account)
        console.log(store.state.accountdatas)
        for (let i = 0; i < accs.length; i++) {
          if (
            accs[i].account_name === '系统仿真账户' &&
            accs00.indexOf(accs[i].account) === -1
          ) {
            for (let i = 0; i < accs.length; i++) {
              accs[i].total_asset = parseInt(accs[i].total_asset)
            }
            store.state.accountdatas.push(accs[i])
          }
          for (let u = 0; u < store.state.accountdatas.length; u++) {
            if (store.state.accountdatas[u].account === '13011111111') {
              store.state.accountdatas[u].acc_nick_name = '蓝色火焰'
            } else if (store.state.accountdatas[u].account === '13055555555') {
              store.state.accountdatas[u].acc_nick_name = '数字创世纪'
            } else if (store.state.accountdatas[u].account === '17601387533') {
              store.state.accountdatas[u].acc_nick_name = '大河策略'
            } else if (store.state.accountdatas[u].account === '13357146917') {
              store.state.accountdatas[u].acc_nick_name = '夏天的风'
            } else if (store.state.accountdatas[u].account === '13077777778') {
              store.state.accountdatas[u].acc_nick_name = '数字军团'
            } else if (store.state.accountdatas[u].account === '13077777780') {
              store.state.accountdatas[u].acc_nick_name = '杏林春暖'
            } else if (store.state.accountdatas[u].account === '13011111115') {
              store.state.accountdatas[u].acc_nick_name = '二进制应用'
            } else if (store.state.accountdatas[u].account === '13077777779') {
              store.state.accountdatas[u].acc_nick_name = '畅聊空间'
            } else {
              store.state.accountdatas[u].acc_nick_name =
                store.state.accountdatas[u].account
            }
            Vue.set(store.state.accountdatas, u, store.state.accountdatas[u])
          }
        }
        store.state.Newdata++
        // for (let c = 0; c < JSON.parse(sharemess).length; c++) {
        //   if (JSON.parse(sharemess)[c].account_name === '系统仿真账户') {
        //     store.state.sharetodayNet =
        //       JSON.parse(sharemess)[c].total_asset / 10000
        //     console.log(store.state.sharetodayNet)
        //   }
        // }
        console.log(JSON.parse(sharemess))
        console.log(store.state.accountdatas)
      } else if (
        store.state.mess1 === 'data_account records begin' &&
        store.state.groupname === 'PROMOTE'
      ) {
        var accs1 = []
        var accs0 = []
        accs1.push(...JSON.parse(sharemess))
        console.log(accs1[0].account)
        console.log(store.state.accountdataspromote)
        for (let m = 0; m < store.state.accountdataspromote.length; m++) {
          accs0.push(store.state.accountdataspromote[m].account)
        }
        for (let i = 0; i < accs1.length; i++) {
          if (
            accs1[i].account_name === '系统仿真账户' &&
            accs0.indexOf(accs1[i].account) === -1 &&
            store.state.promotionaccount.indexOf(accs1[i].account) > -1
          ) {
            for (let i = 0; i < accs1.length; i++) {
              accs1[i].total_asset = parseInt(accs1[i].total_asset)
            }
            store.state.accountdataspromote.push(accs1[i])
          }
        }
        store.state.Newdata++
        // for (let c = 0; c < JSON.parse(sharemess).length; c++) {
        //   if (JSON.parse(sharemess)[c].account_name === '系统仿真账户') {
        //     store.state.sharetodayNet =
        //       JSON.parse(sharemess)[c].total_asset / 10000
        //     console.log(store.state.sharetodayNet)
        //   }
        // }
        console.log(JSON.parse(sharemess))
        console.log(store.state.accountdataspromote)
      } else if (
        store.state.mess1 === 'data_account records change begin' &&
        store.state.groupname === 'FOLLOWERS'
      ) {
        for (let j = 0; j < store.state.accountdatas.length; j++) {
          console.log('测试' + sharemess)
          if (sharemess.charAt(0) === '[') {
            for (let k = 0; k < JSON.parse(sharemess).length; k++) {
              if (
                store.state.accountdatas[j].account ===
                JSON.parse(sharemess)[k].account
              ) {
                store.state.accountdatas[j].total_asset = parseInt(
                  JSON.parse(sharemess)[k].total_asset
                )
                store.state.accountdatas[j].stock_value = JSON.parse(sharemess)[
                  k
                ].stock_value
                store.state.accountdatas[j].balance = JSON.parse(sharemess)[
                  k
                ].balance
                store.state.accountdatas[j].available = JSON.parse(sharemess)[
                  k
                ].available
                store.state.accountdatas[j].frozen_cash = JSON.parse(sharemess)[
                  k
                ].frozen_cash
                Vue.set(
                  store.state.accountdatas,
                  j,
                  store.state.accountdatas[j]
                )
              }
            }
          }
          // for (let i = 0; i < JSON.parse(sharemess).length; i++) {
          //   if (JSON.parse(sharemess)[i].account_name === '系统仿真账户') {
          //     store.state.sharetodayNet =
          //       JSON.parse(sharemess)[i].total_asset / 10000
          //     console.log(store.state.sharetodayNet)
          //   }
          // }
        }
        // store.state.accountdatas.push(...JSON.parse(sharemess))
      } else if (
        store.state.mess1 === 'data_push message begin' &&
        store.state.groupname === 'FOLLOWERS' &&
        obj.SHARE.TOKEN === store.state.promotiontoken[store.state.zheI]
      ) {
        store.state.pushFollowMessage.push(sharemess.slice(11))
      } else if (
        store.state.mess1 === 'data_account records change begin' &&
        store.state.groupname === 'PROMOTE'
      ) {
        console.log(sharemess)
        for (let j = 0; j < store.state.accountdataspromote.length; j++) {
          for (let k = 0; k < JSON.parse(sharemess).length; k++) {
            if (
              store.state.accountdataspromote[j].account ===
              JSON.parse(sharemess)[k].account
            ) {
              store.state.accountdataspromote[j].total_asset = parseInt(
                JSON.parse(sharemess)[k].total_asset
              )
              store.state.accountdataspromote[j].stock_value = JSON.parse(
                sharemess
              )[k].stock_value
              store.state.accountdataspromote[j].balance = JSON.parse(
                sharemess
              )[k].balance
              store.state.accountdataspromote[j].available = JSON.parse(
                sharemess
              )[k].available
              store.state.accountdataspromote[j].frozen_cash = JSON.parse(
                sharemess
              )[k].frozen_cash
              Vue.set(
                store.state.accountdataspromote,
                j,
                store.state.accountdataspromote[j]
              )
            }
          }
          //   for (let i = 0; i < JSON.parse(sharemess).length; i++) {
          //     if (JSON.parse(sharemess)[i].account_name === '系统仿真账户') {
          //       store.state.sharetodayNet =
          //         JSON.parse(sharemess)[i].total_asset / 10000
          //       console.log(store.state.sharetodayNet)
          //     }
          //   }
        }
        // store.state.accountdatas.push(...JSON.parse(sharemess))
      } else if (store.state.mess1 === 'data_transaction records begin') {
        // console.log(sharemess)
        if (obj.SHARE.TOKEN === store.state.promotiontoken[store.state.zheI]) {
          store.state.transactiondatas.push(...JSON.parse(sharemess))
        }
        // 下面是把里面的字符串转成数字方便排序用
        // for (let i = 0; i < store.state.transactiondatas[x].length; i++) {
        //   store.state.transactiondatas[x][
        //     i
        //   ].transaction.open_order.executed_volume = parseInt(
        //     store.state.transactiondatas[x][i].transaction.open_order
        //       .executed_volume
        //   )
        //   store.state.transactiondatas[x][
        //     i
        //   ].transaction.profit_percent = parseInt(
        //     store.state.transactiondatas[x][i].transaction.profit_percent
        //   )
        //   store.state.transactiondatas[x][
        //     i
        //   ].transaction.open_order.executed_volume = parseInt(
        //     store.state.transactiondatas[x][i].transaction.open_order
        //       .executed_volume
        //   )
        //   store.state.transactiondatas[x][
        //     i
        //   ].transaction.open_order.order_volume = parseInt(
        //     store.state.transactiondatas[x][i].transaction.open_order
        //       .order_volume
        //   )
        //   store.state.transactiondatas[x][
        //     i
        //   ].transaction.open_order.order_price = parseInt(
        //     store.state.transactiondatas[x][i].transaction.open_order
        //       .order_price
        //   )
        //   store.state.transactiondatas[x][
        //     i
        //   ].transaction.open_order.order_no = parseInt(
        //     store.state.transactiondatas[x][i].transaction.open_order.order_no
        //   )

        //   // console.log(store.state.transactiondatas)
        // }
      } else if (
        store.state.mess1 === 'data_transaction records profit begin'
      ) {
        console.log('浮动盈亏')
        // for (let i = 0; i < store.state.transactiondatas.length; i++) {
        //   store.state.transactiondatas[
        //     i
        //   ].transaction.profit_percent = sharemess.split(',')[i]
        //   store.state.transactiondatas[i].transaction.profit_percent = parseInt(
        //     store.state.transactiondatas[i].transaction.profit_percent
        //   )
        //   Vue.set(
        //     store.state.transactiondatas,
        //     i,
        //     store.state.transactiondatas[i]
        //   )
        // }
      } else if (store.state.mess1 === 'data_his_asset records begin') {
        // 账户净值
        store.state.sharenetWorthCurve.push(...JSON.parse(sharemess))
        // 账户净值
        console.log(sharemess)
        console.log(store.state.sharenetWorthCurve)
        // console.log(store.state.change)
        for (let i = 0; i < store.state.sharenetWorthCurve.length; i++) {
          store.state.shareTIME.push(store.state.sharenetWorthCurve[i].time)
          store.state.shareNET.push(
            towNumber(store.state.sharenetWorthCurve[i].net_value / 10000)
          )
        }
        console.log(obj.SHARE)
        console.log(store.state.promotiontoken)
        if (obj.SHARE.TOKEN === store.state.promotiontoken[store.state.zheI]) {
          store.state.change++
          console.log(store.state.change)
        }
        console.log(store.state.shareNET)
      }
    }
  } else if (['FOLLOWINGS LIST'] in obj) {
    store.state.followtoken = []
    store.state.followaccount = []
    console.log(Object.keys(obj['FOLLOWINGS LIST']).length)
    for (let i = 0; i < Object.keys(obj['FOLLOWINGS LIST']).length; i++) {
      if (obj['FOLLOWINGS LIST']['ACCOUNT' + i].TOKEN === '') {
        console.log(
          '账户' + obj['FOLLOWINGS LIST']['ACCOUNT' + i].ACCOUNT + '未登录'
        )
        store.state.accountStatus = '未登录'
      } else {
        console.log(obj['FOLLOWINGS LIST']['ACCOUNT' + i].TOKEN)
        store.state.followtoken.push(
          obj['FOLLOWINGS LIST']['ACCOUNT' + i].TOKEN
        )
        store.state.followaccount.push(
          obj['FOLLOWINGS LIST']['ACCOUNT' + i].ACCOUNT
        )
        console.log(store.state.followaccount)
      }
    }
    return store.state.followtoken
  } else if ('ROUTER' in obj) {
    // 接收自己账户的信息
    var sd = Base64.decode(obj.ROUTER.CONTENT)
    console.log(sd)
    if (sd === 'transaction records begin') {
      store.state.data1 = []
      store.state.mess = 'data_transaction records begin'
      // } else if (sd === 'transjob records begin') {
      //   store.state.data2 = []
      //   store.state.mess = 'data_transjob records begin'
    } else if (sd === 'follow account records begin') {
      // 主动请求时的单对单发送账号数据
      store.state.mess = 'data_follow account records begin'
    } else if (sd === 'follow transaction records begin') {
      store.state.data1 = []
      if (obj.ROUTER.TOKEN === store.state.promotiontoken[store.state.zheI]) {
        store.state.transactiondatas = []
      }
      store.state.mess = 'data_follow transaction records begin'
    } else if (sd === 'follow his_asset records begin') {
      store.state.sharenetWorthCurve = []
      store.state.shareNET = []
      store.state.shareTIME = []
      store.state.netWorthCurve = []
      store.state.NET = []
      store.state.TIME = []
      store.state.mess = 'data_follow his_asset records begin'
    } else if (sd === 'account records begin') {
      store.state.data3 = []
      store.state.mess = 'data_account records begin'
    } else if (sd === 'account records change begin') {
      store.state.mess = 'data_account records change begin'
    } else if (sd === 'algo records begin') {
      store.state.data4 = []
      store.state.mess = 'data_algo records begin'
    } else if (sd === 'transaction records profit begin') {
      store.state.mess = 'data_transaction records profit begin'
    } else if (sd === 'his_asset records begin') {
      store.state.netWorthCurve = []
      store.state.TIME = []
      store.state.NET = []
      store.state.mess = 'data_his_asset records begin'
    } else if (sd === 'push message begin') {
      store.state.mess = 'data_push message begin'
    } else if (sd === 'promote account records begin') {
      store.state.mess = 'data_promote account records begin'
    } else {
      if (store.state.mess === 'data_transaction records begin') {
        store.state.data1.push(...JSON.parse(sd))
        for (let i = 0; i < store.state.data1.length; i++) {
          store.state.data1[
            i
          ].transaction.open_order.executed_volume = parseInt(
            store.state.data1[i].transaction.open_order.executed_volume
          )
          store.state.data1[i].transaction.profit_percent = parseInt(
            store.state.data1[i].transaction.profit_percent
          )
          store.state.data1[
            i
          ].transaction.open_order.executed_volume = parseInt(
            store.state.data1[i].transaction.open_order.executed_volume
          )
          store.state.data1[i].transaction.open_order.order_volume = parseInt(
            store.state.data1[i].transaction.open_order.order_volume
          )
          store.state.data1[i].transaction.open_order.order_price = parseInt(
            store.state.data1[i].transaction.open_order.order_price
          )
          store.state.data1[i].transaction.open_order.order_no = parseInt(
            store.state.data1[i].transaction.open_order.order_no
          )

          // console.log(store.state.data1)
        }
      } else if (store.state.mess === 'data_his_asset records begin') {
        store.state.netWorthCurve.push(...JSON.parse(sd))
        // 账户净值
        for (let i = 0; i < store.state.netWorthCurve.length; i++) {
          store.state.TIME.push(store.state.netWorthCurve[i].time)
          store.state.NET.push(
            towNumber(store.state.netWorthCurve[i].net_value / 10000)
          )
        }
        console.log(store.state.NET)
        // } else if (store.state.mess === 'data_transjob records begin') {
        //   store.state.data2.push(...JSON.parse(sd))
      } else if (store.state.mess === 'data_account records begin') {
        if (store.state.peerToken === obj.ROUTER.TOKEN) {
          store.state.data3.push(...JSON.parse(sd))
          for (let i = 0; i < JSON.parse(sd).length; i++) {
            if (JSON.parse(sd)[i].account_name === '系统仿真账户') {
              store.state.todayNet = JSON.parse(sd)[i].total_asset / 10000
            }
          }
          console.log(store.state.todayNet)
          store.state.Newdata++
        }
      } else if (store.state.mess === 'data_account records change begin') {
        for (let j = 0; j < store.state.data3.length; j++) {
          store.state.data3[j].total_asset = parseInt(
            JSON.parse(sd)[j].total_asset
          )
          store.state.data3[j].stock_value = JSON.parse(sd)[j].stock_value
          store.state.data3[j].balance = JSON.parse(sd)[j].balance
          store.state.data3[j].available = JSON.parse(sd)[j].available
          store.state.data3[j].frozen_cash = JSON.parse(sd)[j].frozen_cash
          Vue.set(store.state.data3, j, store.state.data3[j])
          for (let i = 0; i < JSON.parse(sd).length; i++) {
            if (JSON.parse(sd)[i].account_name === '系统仿真账户') {
              store.state.todayNet = JSON.parse(sd)[i].total_asset / 10000
            }
          }
        }
      } else if (store.state.mess === 'data_algo records begin') {
        store.state.data4.push(...JSON.parse(sd))
      } else if (store.state.mess === 'data_transaction records profit begin') {
        console.log(sd)
        for (let i = 0; i < store.state.data1.length; i++) {
          store.state.data1[i].transaction.profit_percent = sd.split(',')[i]
          store.state.data1[i].transaction.profit_percent = parseInt(
            store.state.data1[i].transaction.profit_percent
          )
          Vue.set(store.state.data1, i, store.state.data1[i])
        }
      } else if (store.state.mess === 'data_follow account records begin') {
        accs = []
        accs00 = []
        accs.push(...JSON.parse(sd))
        for (let m = 0; m < store.state.accountdatas.length; m++) {
          accs00.push(store.state.accountdatas[m].account)
        }
        console.log(accs[0].account)
        console.log(store.state.accountdatas)
        for (let i = 0; i < accs.length; i++) {
          if (
            accs[i].account_name === '系统仿真账户' &&
            accs00.indexOf(accs[i].account) === -1
          ) {
            for (let i = 0; i < accs.length; i++) {
              accs[i].total_asset = parseInt(accs[i].total_asset)
            }
            store.state.accountdatas.push(accs[i])
          }
          for (let u = 0; u < store.state.accountdatas.length; u++) {
            if (store.state.accountdatas[u].account === '13011111111') {
              store.state.accountdatas[u].acc_nick_name = '蓝色火焰'
            } else if (store.state.accountdatas[u].account === '13055555555') {
              store.state.accountdatas[u].acc_nick_name = '数字创世纪'
            } else if (store.state.accountdatas[u].account === '17601387533') {
              store.state.accountdatas[u].acc_nick_name = '大河策略'
            } else if (store.state.accountdatas[u].account === '13357146917') {
              store.state.accountdatas[u].acc_nick_name = '夏天的风'
            } else if (store.state.accountdatas[u].account === '13077777778') {
              store.state.accountdatas[u].acc_nick_name = '数字军团'
            } else if (store.state.accountdatas[u].account === '13077777780') {
              store.state.accountdatas[u].acc_nick_name = '杏林春暖'
            } else if (store.state.accountdatas[u].account === '13011111115') {
              store.state.accountdatas[u].acc_nick_name = '二进制应用'
            } else if (store.state.accountdatas[u].account === '13077777779') {
              store.state.accountdatas[u].acc_nick_name = '畅聊空间'
            } else {
              store.state.accountdatas[u].acc_nick_name =
                store.state.accountdatas[u].account
            }
            Vue.set(store.state.accountdatas, u, store.state.accountdatas[u])
          }
        }
        store.state.Newdata++
      } else if (
        store.state.mess === 'data_follow transaction records begin' &&
        store.state.routerPath !== '/mobliedata3'
      ) {
        if (obj.ROUTER.TOKEN === store.state.promotiontoken[store.state.zheI]) {
          store.state.transactiondatas.push(...JSON.parse(sd))
        }
      } else if (
        store.state.mess === 'data_follow transaction records begin' &&
        store.state.routerPath === '/mobliedata3'
      ) {
        store.state.data1.push(...JSON.parse(sd))
        for (let i = 0; i < store.state.data1.length; i++) {
          store.state.data1[
            i
          ].transaction.open_order.executed_volume = parseInt(
            store.state.data1[i].transaction.open_order.executed_volume
          )
          store.state.data1[i].transaction.profit_percent = parseInt(
            store.state.data1[i].transaction.profit_percent
          )
          store.state.data1[
            i
          ].transaction.open_order.executed_volume = parseInt(
            store.state.data1[i].transaction.open_order.executed_volume
          )
          store.state.data1[i].transaction.open_order.order_volume = parseInt(
            store.state.data1[i].transaction.open_order.order_volume
          )
          store.state.data1[i].transaction.open_order.order_price = parseInt(
            store.state.data1[i].transaction.open_order.order_price
          )
          store.state.data1[i].transaction.open_order.order_no = parseInt(
            store.state.data1[i].transaction.open_order.order_no
          )

          // console.log(store.state.data1)
        }
      } else if (
        store.state.mess === 'data_follow his_asset records begin' &&
        store.state.routerPath !== '/mobliedata3'
      ) {
        // 账户净值
        store.state.sharenetWorthCurve.push(...JSON.parse(sd))
        // 账户净值
        console.log(sd)
        console.log(store.state.sharenetWorthCurve)
        // console.log(store.state.change)
        for (let i = 0; i < store.state.sharenetWorthCurve.length; i++) {
          store.state.shareTIME.push(store.state.sharenetWorthCurve[i].time)
          store.state.shareNET.push(
            towNumber(store.state.sharenetWorthCurve[i].net_value / 10000)
          )
        }
        if (obj.ROUTER.TOKEN === store.state.promotiontoken[store.state.zheI]) {
          store.state.change++
          console.log(store.state.change)
        }
        console.log(store.state.shareNET)
      } else if (
        store.state.mess === 'data_follow his_asset records begin' &&
        store.state.routerPath === '/mobliedata3'
      ) {
        store.state.netWorthCurve.push(...JSON.parse(sd))
        // 账户净值
        for (let i = 0; i < store.state.netWorthCurve.length; i++) {
          store.state.TIME.push(store.state.netWorthCurve[i].time)
          store.state.NET.push(
            towNumber(store.state.netWorthCurve[i].net_value / 10000)
          )
        }
        console.log(store.state.NET)
      } else if (store.state.mess === 'data_push message begin') {
        store.state.pushMessage.push(sd.slice(11))
        console.log(store.state.pushMessage)
      } else if (
        store.state.mess === 'data_promote account records begin' &&
        store.state.routerPath === '/mobliedata3'
      ) {
        store.state.data3 = []
        console.log(123)
        if (store.state.peerToken === obj.ROUTER.TOKEN) {
          store.state.data3.push(...JSON.parse(sd))
          for (let i = 0; i < JSON.parse(sd).length; i++) {
            if (JSON.parse(sd)[i].account_name === '系统仿真账户') {
              store.state.todayNet = JSON.parse(sd)[i].total_asset / 10000
            }
          }
          console.log(store.state.todayNet)
          store.state.Newdata++
        }
      } else if (
        store.state.mess === 'data_promote account records begin' &&
        store.state.routerPath !== '/mobliedata3'
      ) {
        console.log(store.state.routerPath)
        var accs11 = []
        var accs000 = []
        accs11.push(...JSON.parse(sd))
        console.log(accs11)
        console.log(store.state.accountdataspromote)
        for (let mm = 0; mm < store.state.accountdataspromote.length; mm++) {
          accs000.push(store.state.accountdataspromote[mm].account)
        }
        for (let i = 0; i < accs11.length; i++) {
          if (
            accs11[i].account_name === '系统仿真账户' &&
            accs000.indexOf(accs11[i].account) === -1 &&
            store.state.promotionaccount.indexOf(accs11[i].account) > -1
          ) {
            for (let i = 0; i < accs11.length; i++) {
              accs11[i].total_asset = parseInt(accs11[i].total_asset)
            }
            store.state.accountdataspromote.push(accs11[i])
          }
          for (let u = 0; u < store.state.accountdataspromote.length; u++) {
            if (store.state.accountdataspromote[u].account === '13011111111') {
              store.state.accountdataspromote[u].acc_nick_name = '蓝色火焰'
            } else if (
              store.state.accountdataspromote[u].account === '13055555555'
            ) {
              store.state.accountdataspromote[u].acc_nick_name = '数字创世纪'
            } else if (
              store.state.accountdataspromote[u].account === '17601387533'
            ) {
              store.state.accountdataspromote[u].acc_nick_name = '大河策略'
            } else if (
              store.state.accountdataspromote[u].account === '13357146917'
            ) {
              store.state.accountdataspromote[u].acc_nick_name = '夏天的风'
            } else if (
              store.state.accountdataspromote[u].account === '13077777778'
            ) {
              store.state.accountdataspromote[u].acc_nick_name = '数字军团'
            } else if (
              store.state.accountdataspromote[u].account === '13077777780'
            ) {
              store.state.accountdataspromote[u].acc_nick_name = '杏林春暖'
            } else if (
              store.state.accountdataspromote[u].account === '13011111115'
            ) {
              store.state.accountdataspromote[u].acc_nick_name = '二进制应用'
            } else if (
              store.state.accountdataspromote[u].account === '13077777779'
            ) {
              store.state.accountdataspromote[u].acc_nick_name = '畅聊空间'
            } else {
              store.state.accountdataspromote[u].acc_nick_name =
                store.state.accountdataspromote[u].account
            }
            Vue.set(
              store.state.accountdataspromote,
              u,
              store.state.accountdataspromote[u]
            )
          }
        }
        console.log(store.state.accountdatas)
        var accs2 = []
        var accs002 = []
        accs2.push(...JSON.parse(sd))
        for (let m = 0; m < store.state.accountdatas.length; m++) {
          accs002.push(store.state.accountdatas[m].account)
        }
        console.log(accs2[0].account)
        console.log(store.state.accountdatas)
        for (let i = 0; i < accs2.length; i++) {
          if (
            accs2[i].account_name === '系统仿真账户' &&
            accs002.indexOf(accs2[i].account) === -1
          ) {
            for (let i = 0; i < accs2.length; i++) {
              accs2[i].total_asset = parseInt(accs2[i].total_asset)
            }
            store.state.accountdatas.push(accs2[i])
          }
          for (let u = 0; u < store.state.accountdatas.length; u++) {
            if (store.state.accountdatas[u].account === '13066666676') {
              store.state.accountdatas[u].acc_nick_name = '蓝色天空'
            } else if (store.state.accountdatas[u].account === '13011111111') {
              store.state.accountdatas[u].acc_nick_name = '蓝色火焰'
            } else if (store.state.accountdatas[u].account === '13055555555') {
              store.state.accountdatas[u].acc_nick_name = '数字创世纪'
            } else if (store.state.accountdatas[u].account === '13055555575') {
              store.state.accountdatas[u].acc_nick_name = '蓝色方程'
            } else if (store.state.accountdatas[u].account === '17601387533') {
              store.state.accountdatas[u].acc_nick_name = '大河策略'
            } else if (store.state.accountdatas[u].account === '13357146917') {
              store.state.accountdatas[u].acc_nick_name = '夏天的风'
            } else if (store.state.accountdatas[u].account === '13011111114') {
              store.state.accountdatas[u].acc_nick_name = '蓝色海洋'
            } else {
              store.state.accountdatas[u].acc_nick_name =
                store.state.accountdatas[u].account
            }
            Vue.set(store.state.accountdatas, u, store.state.accountdatas[u])
          }
          console.log(store.state.accountdatas)
        }
      }
    }
  }
  // 小数点后保留两位的函数
  function towNumber(val) {
    return Number(val).toFixed(2)
  }
  // 判断接收到的指令将数据存放到不同表格对应的数组中去
}
const oncloseWS = e => {
  console.info(e)
  console.log('closed')
}
const onerrorWS = e => {
  console.info(e)
  reconnect(wsUrl)
  console.log('发生异常')
}
function reconnect(url) {
  if (lockReconnect) return
  lockReconnect = true
  setTimeout(function() {
    // 没连接上会一直重连，设置延迟避免请求过多
    console.log('这个断线重连机制')
    createSocket(url)
    lockReconnect = false
  }, 5000)
}
// 心跳检测
var heartCheck = {
  timeout: 10000, // 发一次心跳的时间，单位毫秒
  timeoutObj: null,
  serverTimeoutObj: null,
  reset: function() {
    clearTimeout(this.timeoutObj)
    clearTimeout(this.serverTimeoutObj)
    return this
  },
  start: function() {
    var self = this
    this.timeoutObj = setTimeout(function() {
      // 这里发送一个心跳，后端收到后，返回一个心跳消息，
      // onmessage拿到返回的心跳就说明连接正常
      var messageTOKENLIST1 = {
        'TOKEN LIST': {
          TOKEN: store.state.token,
          ENCODING: 'UTF-8'
        }
      }
      var messageJsonTOKENLIST1 = JSON.stringify(messageTOKENLIST1)
      Socket.send(messageJsonTOKENLIST1)
      console.log('ping!')
      self.serverTimeoutObj = setTimeout(function() {
        // 如果超过一定时间还没重置，说明后端主动断开了
        Socket.close() // 如果onclose会执行reconnect，我们执行ws.close()就行了.如果直接执行reconnect 会触发onclose导致重连两次
      }, self.timeout)
    }, this.timeout)
  }
}
